#include "SpriteScene.h"
#include "publicDefine.h"

Scene* SpriteScene::createScene()
{
	return SpriteScene::create();
}

bool SpriteScene::init()
{
	if (!Scene::init())
	{
		return false;
	}

	SpriteFrameCache::getInstance()->addSpriteFramesWithFile("0911/cityscene.plist", "0911/cityscene.png");
	SpriteFrameCache::getInstance()->addSpriteFramesWithFile("0912/myRes.plist", "0912/myRes.png");
	SpriteFrameCache::getInstance()->addSpriteFramesWithFile("0912/dazhao.plist", "0912/dazhao.png");
	SpriteFrameCache::getInstance()->addSpriteFramesWithFile("0912/dead.plist", "0912/dead.png");

	auto frames_fish = SpriteScene::getSpriteFrames("fish%d.png", 9, 0);

	Animation* animation_fish = Animation::createWithSpriteFrames(frames_fish, 1.0f / 10);

	Animate* animate_fish = Animate::create(animation_fish);

	Sprite* _sprFish = Sprite::createWithSpriteFrame(frames_fish.front());
	_sprFish->setPosition(VISIBLE_SIZE.width / 2, VISIBLE_SIZE.height / 2);
	addChild(_sprFish);

	_sprFish->runAction(RepeatForever::create(animate_fish));



	auto frames_dazhao = SpriteScene::getSpriteFrames("dazhao_%d.png", 87, 1);

	Animation* animation_dazhao = Animation::createWithSpriteFrames(frames_dazhao, 3.0f / 87);

	Animate* animate_dazhao = Animate::create(animation_dazhao);

	Sprite* _sprDazhao = Sprite::createWithSpriteFrame(frames_dazhao.front());
	_sprDazhao->setPosition(VISIBLE_SIZE.width / 4, VISIBLE_SIZE.height / 4);
	addChild(_sprDazhao);

	_sprDazhao->runAction(RepeatForever::create(animate_dazhao));



	auto frames_dead = SpriteScene::getSpriteFrames("dead_%d.png", 23, 1);

	Animation* animation_dead = Animation::createWithSpriteFrames(frames_dead, 3.0f / 23);

	Animate* animate_dead = Animate::create(animation_dead);

	Sprite* _sprDead = Sprite::createWithSpriteFrame(frames_dead.front());
	_sprDead->setPosition(VISIBLE_SIZE.width / 3, VISIBLE_SIZE.height / 4);
	_sprDead->setScaleX(-1);
	addChild(_sprDead);

	_sprDead->runAction(RepeatForever::create(animate_dead));
	/*Sprite* _sprBg = Sprite::createWithSpriteFrameName("background.png");
	_sprBg->setPosition(VISIBLE_SIZE.width / 2, VISIBLE_SIZE.height / 2);
	addChild(_sprBg);

	auto frams = SpriteScene::getSpriteFrames("capguy/walk/%04d.png", 8);

	Sprite* _sprPerson = Sprite::createWithSpriteFrame(frams.front());
	_sprPerson->setPosition(VISIBLE_SIZE.width / 4, VISIBLE_SIZE.height / 4);
	addChild(_sprPerson);

	Animation* animation = Animation::createWithSpriteFrames(frams, 1.0f / 8);*/

	/*for (int i = 0; i < frams.size(); i++) {
		animation->addSpriteFrame(frams.at(i));
	}*/

	/*Animate* animate = Animate::create(animation);

	MoveTo* moveTo = MoveTo::create(5, Vec2(VISIBLE_SIZE.width,VISIBLE_SIZE.height));
	MoveBy* moveBy = MoveBy::create(5, Vec2(100, 100));
	TintTo* tintTo = TintTo::create(5, Color3B(255, 0, 0));
	TintBy* tintBy = TintBy::create(5, 55,88,99);
	BezierTo* bezierTo = BezierTo::create(1, {Vec2(100,100),Vec2(800,800),Vec2(100,100)});

	_sprPerson->runAction(RepeatForever::create(animate));*/

	return true;
}

Vector<SpriteFrame*> SpriteScene::getSpriteFrames(char* format, int frameCount, int startIdx)
{
	Vector<SpriteFrame*> resultVec;

	for (int i = startIdx; i <= frameCount; i++) {
		SpriteFrame* sf = SpriteFrameCache::getInstance()->getSpriteFrameByName(StringUtils::format(format, i));
		resultVec.pushBack(sf);
	}

	return resultVec;
}
